<?php

/*
 * This class is used for as Model for Department table 
 */

class Model_Department extends Model_Base {

    protected $_name = 'tbl_department';
    protected $_primaryKey = 'dep_id';

    /**
     * Function to return different units of the Department from the uom table
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    //get siteing data
    public function getsiteingArray() {
        $siteingModel = new Model_Building();
        $siteingArray = $siteingModel->fetchBuilding();
        $finalArray[''] = '';
        foreach ($siteingArray as $com) {
            $finalArray[$com['site_id']] = $com['site_name'];
        }
        return $finalArray;
    }
    
    
    //get site data
    public function getsiteArray() {
        $siteModel = new Model_Site();
        $siteArray = $siteModel->fetchSite();
        $finalArray[''] = '';
        foreach ($siteArray as $com) {
            $finalArray[$com['site_id']] = $com['site_name'];
        }
        return $finalArray;
    }

    //get country data
    public function getcountryArray() {
        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchall();
        $finalArray[''] = '-- Select Country --';
        foreach ($countryArray as $com) {
            $finalArray[$com['country_id']] = $com['country_name'];
        }
        return $finalArray;
    }

    /**
     * Function to return different status of the Department
     *
     * @param  null
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function getStatusArray() {
        return array(
            #'' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    /**
     * Fetch an individual entry for view
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryByViewId($id) {

        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('department' => 'tbl_department'))
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = department.updated_lgn_id")
                ->join(array('site' => 'tbl_site'), "site.site_id = department.site_id")
                ->where('dep_id = ?', $id)
                ->where('dep_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array
        #echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('department' => 'tbl_department'))
                ->setIntegrityCheck(false)
                ->where('dep_id = ?', $id)
                ->where('dep_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array

        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get departments
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchDepartment($where = "", $limit = "", $offset = "0", $order = 'dep_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('department' => 'tbl_department'))
                ->setIntegrityCheck(false)
                ->join(array('site' => 'tbl_site'), "site.site_id = department.site_id")
                ->where('dep_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        $data = parent::fetchAll($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total orders
     * @return int
     */
    public function getDepartmentCount($where) {
        $select = $this->select()
                ->from(array('department' => 'tbl_department'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('site' => 'tbl_site'), "site.site_id = department.site_id")
                ->where('dep_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        $rows = parent::fetchRow($select);
        return($rows->cnt);
    }

    /**
     *  This function is used to delete departments
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteDepartment($prdtArray) {

        if (is_array($prdtArray)) {
            foreach ($prdtArray as $_department) {
                $departmentData = Array();
                $departmentData = $this->fetchEntryById($_department);
                if (count($departmentData) > 0) {
                    $data = array('dep_status' => '2');
                    $this->update($data, 'dep_id =' . (int) $_department);
                }
            }
        } else {
            $dep_id = $prdtArray;
            $departmentData = $this->fetchEntryById($dep_id);
            if (count($departmentData) > 0) {

                $data = array('dep_status' => '2');
                $this->update($data, 'dep_id =' . $dep_id);
            }
        }
    }

    
    public function getsitecompanyArray($SITEId) {
         $finalArray = array();
         $siteModel = new Model_Site();
         
         
         $where ="site.site_id IN (".$SITEId.")";
         $siteArray = $siteModel->fetchSite($where);
         
         foreach($siteArray as $key =>$value){
             $companyModel = new Model_Company();
             $where_com ="com_id = ".$value['com_id'];
             $companyData = $companyModel->fetchCompany($where_com);
             foreach($companyData as $ckey =>$cvalue){
               // if(!in_array($cvalue['com_name'], $finalArray)) {
                    $finalArray[$cvalue['com_id']]['id'] = $cvalue['com_id'];
                    $finalArray[$cvalue['com_id']]['name'] = $cvalue['com_name'];
               /// }
                
             }
                // $finalArray['company'][$key] = $value['com_id'];
         }
        
         return $finalArray;
         
        //exit;
    }
}
